import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { List, Card, Button, Modal, Form, Select } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import Result from '@/components/Result';
import moment from 'moment';
import styles from './Withdrawal.less';

const FormItem = Form.Item;
const { Option } = Select;

@connect(({ withdrawal, loading }) => ({
  withdrawal,
  loading: loading.models.list,
}))
@Form.create()
class Withdrawal extends PureComponent {
  state = {
    visible: false,
    done: false,
  };

  formLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 13 },
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'withdrawal/fetch',
    });
  }

  showModal = () => {
    this.setState({
      visible: true,
      current: undefined,
    });
  };

  showEditModal = (item, index) => {
    this.setState({
      visible: true,
      current: item,
      editableIndex: index,
    });
  };

  handleDone = () => {
    this.setState({
      done: false,
      visible: false,
    });
  };

  handleCancel = () => {
    this.setState({
      visible: false,
    });
  };

  handleSubmit = e => {
    e.preventDefault();
    const { dispatch, form } = this.props;
    const { current, editableIndex } = this.state;
    const id = current ? current.id : '';
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      this.setState({
        done: true,
      });
      dispatch({
        type: 'withdrawal/update',
        payload: {
          ...fieldsValue,
          id,
          editableIndex,
        },
      });
    });
  };

  deleteItem = (id, currentIndex) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'withdrawal/remove',
      payload: currentIndex,
    });
  };

  render() {
    const {
      withdrawal: { data },
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
    const { visible, done, current = {} } = this.state;

    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };

    const ListContent = ({
      data: { phone, price, bank, createdAt, wechatAccountOpenId, alipayName, alipayAccount },
    }) => (
      <div className={styles.listContent}>
        <div className={styles.listContentItem}>
          <span>手机号</span>
          <p>{phone || '暂无'}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>金额</span>
          <p>{price / 100}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>时间</span>
          <p>{moment(createdAt).format('YYYY-MM-DD')}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>微信ID</span>
          <p>{wechatAccountOpenId || '暂无'}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>提现姓名</span>
          <p>{alipayName || '暂无'}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>开户行</span>
          <p>{bank || '暂无'}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>提现账号</span>
          <p>{alipayAccount || '暂无'}</p>
        </div>
      </div>
    );

    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="已确认添加该图片"
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
            className={styles.formResult}
          />
        );
      }
      // const imageUrl = this.state.imageUrl;
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem label="确定审核" {...this.formLayout}>
            {getFieldDecorator('status', {})(
              <Select style={{ width: 120 }}>
                <Option value="success">通过</Option>
                <Option value="jujue">拒绝</Option>
              </Select>
            )}
          </FormItem>
        </Form>
      );
    };
    return (
      <PageHeaderWrapper>
        <div className={styles.standardList}>
          <Card
            className={styles.listCard}
            bordered={false}
            title="审核管理"
            style={{ marginTop: 24 }}
            bodyStyle={{ padding: '0 32px 40px 32px' }}
          >
            <List
              size="large"
              rowKey="id"
              loading={loading}
              pagination={{
                ...data.pagination,
                onChange: page => {
                  const { dispatch } = this.props;
                  dispatch({
                    type: 'withdrawal/fetch',
                    payload: page,
                  });
                },
              }}
              dataSource={data.list}
              item={data.list}
              renderItem={(item, index) => (
                <List.Item
                  actions={[
                    <a
                      onClick={e => {
                        e.preventDefault();
                        this.showEditModal(item, index);
                      }}
                    >
                      {item.status === 'success' ? '' : '审核'}
                    </a>,
                  ]}
                >
                  <List.Item.Meta title={`[${item.id}] ${item.name}`} description={item.intro} />
                  <ListContent data={item} />
                </List.Item>
              )}
            />
          </Card>
        </div>
        <Modal
          title={done ? null : `审核${current ? '管理' : '编辑'}`}
          className={styles.standardListForm}
          width={640}
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
      </PageHeaderWrapper>
    );
  }
}

export default Withdrawal;
